package com.medical.data.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.excel.EasyExcel;
import com.medical.data.domain.FormData;
import com.medical.data.service.FormDataService;
import com.medical.data.service.UploadService;
import com.medical.data.service.impl.FormDataServiceImpl;
import com.medical.data.utils.Result;
import com.medical.data.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front/formData")
@Api(description = "表单填写数据管理")
@Slf4j
public class FormDataController {
    @Autowired
    private FormDataService formDataService;

    @Autowired
    private UploadService uploadService;


    @ApiOperation("提交表单数据")
    @PostMapping("/addFormData")
    public Result addFormData(@Validated @RequestBody FormData formData, HttpServletRequest request) {
        log.info("/front/formData/addFormData参数========>{},{}", formData, request);
        formDataService.addFormData(formData, request);
        return Result.ok();
    }

    @ApiOperation("获取表单数据")
    @PostMapping("/getFormData")
    public Result getFormData(@Validated @RequestBody QueryFormDataVo queryFormDataVo, HttpServletRequest request) {
        log.info("/front/formData/getFormData参数========>{}", queryFormDataVo, request);
        Map<String, Object> map = formDataService.getFormData(queryFormDataVo, request);
        return Result.ok().data(map);
    }

    @ApiOperation("获取表单数据详情")
    @PostMapping("/getFormDataDetail")
    public Result getFormDataDetail(@RequestBody GetFormDataVo getFormDataVo) {
        log.info("/front/formData/getFormDataDetail参数========>{}", getFormDataVo);
        Map<String, Object> map = formDataService.getFormDataByPatient(getFormDataVo);
        return Result.ok().data(map);
    }

    @ApiOperation("上传图片")
    @PostMapping("/upload/{file}/{formId}")
    @PermitAll
    public Result uploadFormFile(@PathVariable("file") MultipartFile file, @PathVariable("formId") String formId, HttpServletRequest request) throws IOException {
        log.info("/front/formData/upload参数========>{}", file, formId);
        String url = uploadService.uploadImage(file, formId, request, "form");
        return Result.ok().data("url", url);
    }


    @ApiOperation("导出数据")
    @PostMapping("/export")
    @PermitAll
    public Result export(@RequestBody ExportVo exportVo) {
        log.info("/front/formData/export参数========>{}", exportVo);
        List<Map<String, String>> formData = formDataService.exportFormData(exportVo);
        return Result.ok().data("data", formData);
    }
}
